/*
 * @Author: 四川华西绿舍建材有限公司
 * @Date: 2023-11-28 13:59:19
 * @LastEditors: 陈波
 * @LastEditTime: 2024-03-01 21:36:25
 * @Description: 
 * @FilePath: \graDesign\src\store\menuDataSource.ts
 */
import { defineStore } from "pinia";
import { allMenuInfo } from '@/layout/allMenuData';
import { menuInfo } from '@/layout/menuData';
import type { menuInfoType } from '@/layout/allMenuData';
import { getAuthorizedHrefs } from '@/utils/util'

const useMenuDataStore: any = defineStore("menuDataStore", {
  state: () => {
    return {
        // 所有菜单数据(父)
        allMenuInfo,
        // 左侧menu数据(子)
        menuInfo
    }
  },
  getters: {
    // 获取一级菜单列表
    firstMenusList() {
      return this.allMenuInfo.map((item: menuInfoType) => ({ 
        id: item.id,
        code: item.code,
        name: item.name,
        href: item.href || ''
      }))
    },
    // 权限列表
    getAuthMenuInfo() {
      return this.menuInfo;
    },
    // 返回一个数组包含所有权限页面的路径
    getAuthHrefs() {
      return getAuthorizedHrefs(this.allMenuInfo);
    }
  },
  actions: {
    // 处理点击每一项一级菜单事件: 筛选出子项渲染menu列表
    toggleFirstMenu(code: string) {
        const target = this.allMenuInfo.find((item: menuInfoType) => item.code === code);
        if (target) {
          // console.log('target', target)
          // target.path && router.push(target.path)
          this.menuInfo = target.childrenSubMenus;
        }
    }
  }
});

export default useMenuDataStore;